#!/usr/bin/perl
#setpasswd.cgi

require 'setpass-lib.pl';

&ReadParse();

my $error_input = undef;
my $error_invaliduser = undef;
my $error_msg = "";
my ($logminor, $logpriority, $event, $formatstr);

#&header($text{'setpasswd_title'}, "", undef, 0, 1);

unless ($in{'txt_newpassword'} eq $in{'txt_confirmnewpassword'})
{
	$error_input = $error_input."<p>".$text{'setpasswd_msg_passwdnotconfirm'};
}

unless ($in{'txt_newusername'})
{
	$error_input = $error_input."<p>".$text{'setpasswd_msg_newnameerror'};
	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_warning'};
	$event = $text{'naslog_event_fail_by_newname_empty'};
	$formatstr = $remote_user." ".$text{'setpass_naslog_update_fail_by_newname_empty'};
}

$error_msg = $error_input;
unless ($error_msg)
{
	$error_msg = &check_simbol_uname($in{'txt_newusername'});
	$logminor = $text{'module_name'};
	$logpriority = $text{'naslog_warning'};
	$event = $text{'naslog_event_fail_by_nameformat'};
	$formatstr = $remote_user." ".$text{'setpass_naslog_update_fail_by_nameformat'};
}

unless ($error_msg)
{
	unless ($remote_user eq $in{'txt_newusername'})
	{
		if(&is_exist_webmin_user($in{'txt_newusername'}))
		{
			$error_msg = $text{"error_msg_newname_exist"};
			$logminor = $text{'module_name'};
			$logpriority = $text{'naslog_warning'};
			$event = $text{'naslog_event_fail_by_newname_exist'};
			$formatstr = $remote_user." ".$text{'setpass_naslog_update_fail_by_newname_exist'};
		}
	}
}

unless ($error_msg)
{
	my $is_vailad_user = &check_passwd($remote_user , $in{'txt_currentpassword'});

	if($is_vailad_user eq 1)
	{
		local(%users_detail);
		%users_detail = undef;
		$users_detail{'name'} = $in{'txt_newusername'};
		$users_detail{'pass'} = &crt_cryptpasswd($in{'txt_newpassword'});
		$users_detail{'sync'} = 0;
		$users_detail{'cert'} = undef;
		$users_detail{'allow'} = undef;
		$users_detail{'deny'} = undef;

		#@module = &get_webmin_default_acl();

		@module = &get_webmin_user_acl($remote_user);

		&modify_user($remote_user, \%users_detail);

		&save_webmin_acl($in{'txt_newusername'}, $in{'txt_newusername'},\@module);
		&restart_miniserv();

		sleep(1);

		$logminor = $text{'module_name'};
		$logpriority = $text{'naslog_info'};
		$event = $text{'naslog_event_success'};
		$formatstr = $remote_user." ".$text{'setpass_naslog_update_success'}." "."New user name is $in{'txt_newusername'}";
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
		#print "$text{'setpasswd_msg_success'}";
		&redirect( "/session_login.cgi?logout=1" );

	}
	else
	{
		#print("<p> $text{'setpasswd_msg_invalidusr'}\n");
		$error_invaliduser = $text{"setpasswd_msg_invalidusr"};
		$logminor = $text{'module_name'};
		$logpriority = $text{'naslog_warning'};
		$event = $text{'naslog_event_fail_by_checkpass'};
		$formatstr = $remote_user." ".$text{'setpass_naslog_update_fail_by_checkpass'};		
	}
}
else
{
	#printf $error_input;
}


#&header($text{'setpasswd_title'}, "", undef, 0, 1);

if($error_msg)
{
	if ($event)
	{
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}
	&error_setup($text{"error_title_setpass"});
	&error($error_msg);
}
else
{
	if(defined($error_invaliduser))
	{
		if($event)
		{
			&nas_syslog($logminor, $logpriority, $event, $formatstr);
		}
		
		&error_setup($text{"error_title_setpass"});
		&error($error_invaliduser);
	}
}
#&footer("/", $text{'index'});
